<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">
    <el-form-item label="热水计量器编号" prop="vmeterCode">
      <el-input v-model="dataForm.vmeterCode" placeholder="热水计量器编号"></el-input>
    </el-form-item>
    <el-form-item label="热水计量器名称" prop="vmeterName">
      <el-input v-model="dataForm.vmeterName" placeholder="热水计量器名称"></el-input>
    </el-form-item>
<!--    <el-form-item label="状态" prop="meterStatus">
      <el-input v-model="dataForm.meterStatus" placeholder="热水计量器状态 1正常 0异常"></el-input>
    </el-form-item>-->
    <el-form-item label="进水口" prop="meterCodeIn">
      <el-select v-model="dataForm.meterCodeIn"  placeholder="请选择"  filterable style="width:100%">
        <el-option
          v-for="(item, index) in meterInfoList" :key="index"
          :label="item.meterName +'['+item.meterCode+']'"
          :value="item.meterCode">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出水口" prop="meterCodeOut">
      <el-select v-model="dataForm.meterCodeOut"  placeholder="请选择"  filterable style="width:100%" >
        <el-option
          v-for="(item, index) in meterInfoList" :key="index"
          :label="item.meterName +'['+item.meterCode+']'"
          :value="item.meterCode">
        </el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        meterInfoList:[],
        visible: false,
        dataForm: {
          id: 0,
          vmeterCode: '',
          vmeterName: '',
          meterStatus: '',
          meterCodeIn: '',
          meterCodeOut: '',
          projectId: ''
        },
        dataRule: {
          vmeterCode: [
            { required: true, message: '热水计量器编号不能为空', trigger: 'blur' }
          ],
          vmeterName: [
            { required: true, message: '热水计量器名称不能为空', trigger: 'blur' }
          ],
          meterStatus: [
            { required: true, message: '热水计量器状态 1正常 0异常不能为空', trigger: 'blur' }
          ],
          meterCodeIn: [
            { required: true, message: '进水口不能为空', trigger: 'blur' }
          ],
          meterCodeOut: [
            { required: true, message: '出水口不能为空', trigger: 'blur' }
          ],
          projectId: [
            { required: true, message: '项目编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,meterInfoList) {
        this.meterInfoList = meterInfoList
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecshotmeterinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.vmeterCode = data.ecshotmeterinfo.vmeterCode
                this.dataForm.vmeterName = data.ecshotmeterinfo.vmeterName
                this.dataForm.meterStatus = data.ecshotmeterinfo.meterStatus
                this.dataForm.meterCodeIn = data.ecshotmeterinfo.meterCodeIn
                this.dataForm.meterCodeOut = data.ecshotmeterinfo.meterCodeOut
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecshotmeterinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'vmeterCode': this.dataForm.vmeterCode,
                'vmeterName': this.dataForm.vmeterName,

                'meterCodeIn': this.dataForm.meterCodeIn,
                'meterCodeOut': this.dataForm.meterCodeOut,
                "customerId": this.$util.cookies.get('customer_id'),
                'projectId': this.$util.cookies.get("cuuren_projectId")
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
